library PetGrowth initializer init requires LegacySystem

	private function PetGrowthConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A018'
	endfunction

	private function PetGrowthActions takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer i = GetConvertedPlayerId(p)
		local real r
		local integer A = 1
		set udg_PetGrowth[i] = udg_PetGrowth[i] + 1
		if udg_Mode==1 then
			set udg_Experience[i]=udg_Experience[i]+1.0
		endif
		call SetItemCharges(UnitItemInSlot(u,0),GetItemCharges(UnitItemInSlot(u,0))+1)
		call ShiftMoveSpeed(u,1.)
		if udg_PetGrowth[i]==udg_PetGrowthHP[i] then
			set udg_PetGrowthHP[i]=udg_PetGrowthHP[i]+10
			call SetPlayerTechResearched(p,'R00N',GetPlayerTechCount(p,'R00N',true)+1)
		endif
		if udg_PetGrowth[i]==10 then
			call UnitAddAbility(u,'A05N') //Hop
		elseif udg_PetGrowth[i]==20 then
			call UnitAddAbility(u,'A03X') //Bite of Fury
		elseif udg_PetGrowth[i]==30 then //Devour & share vision
			loop
				exitwhen A>udg_Players
				call UnitShareVision(u,Player(udg_Sorted[A]),true)
				set A=A+1
			endloop
		elseif udg_PetGrowth[i]==40 then //Scratch Behind Ears
			//call TriggerRegisterUnitEvent(gg_trg_Scratch_Behind_Ears,u,EVENT_UNIT_SPELL_FINISH)
		elseif udg_PetGrowth[i]==60 then
			call UnitAddAbility(u,'A051') //Sharp Teeth
			call UnitRemoveAbility(u,'Apiv') //Permanent Invisibility
			call SetPlayerHandicap(Player(13),GetPlayerHandicap(Player(13))+0.03)
			call SetPlayerHandicap(Player(14),GetPlayerHandicap(Player(14))+0.03)
		elseif udg_PetGrowth[i]==70 then //Play Dead
			//call TriggerRegisterUnitEvent(gg_trg_Play_Dead,u,EVENT_UNIT_SPELL_EFFECT)
		elseif udg_PetGrowth[i]==100 then
			call UnitRemoveAbility(u,'A01B') //Frenzy
			call UnitAddAbility(u,'A05M') //Grow
		elseif udg_PetGrowth[i]==120 then
			call IncUnitAbilityLevel(u,'A03X')
		elseif udg_PetGrowth[i]==130 then
			call IncUnitAbilityLevel(u,'A02F')
		endif
		if udg_PetGrowth[i]<200 then
			set r = I2R(udg_PetGrowth[i])
			call SetUnitScale( u,1.0/Pow(0.99,r),1.0/Pow(0.99,r),1.0/Pow(0.99,r) )
		endif
		set u = null
	endfunction

	//===========================================================================
	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t,Condition(function PetGrowthConditions))
		call TriggerAddAction(t,function PetGrowthActions)
	endfunction


endlibrary